package com.joysoft.test.webview;

import com.example.mytest.R;
import com.example.mytest.R.id;
import com.example.mytest.R.layout;
import com.joysoft.test.UIHelper;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class WebViewAndListView extends Activity {

	private ListView listView;
	private WebView webView;
	
	String tag = this.getClass().getSimpleName();

	@SuppressLint("SetJavaScriptEnabled") @Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_webview);

		webView = (WebView) findViewById(R.id.webview);
		webView.setInitialScale(0);
		// webview加载
		WebSettings settings = webView.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setAppCacheEnabled(true);
		settings.setAppCacheMaxSize(1024*1024*8); 
		settings.setAppCachePath(getCacheDir().getAbsolutePath());
		settings.setAppCacheEnabled(true); 
		settings.setCacheMode(WebSettings.LOAD_DEFAULT);
		
		findViewById(R.id.webview_ll).setLayerType(View.LAYER_TYPE_SOFTWARE,
				null);

		webView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
			@Override
			public void onScaleChanged(WebView view, float oldScale,
					float newScale) {
				// TODO Auto-generated method stub
				super.onScaleChanged(view, oldScale, newScale);
				Log.e(tag, "oldScale:"+oldScale+" newScale:"+newScale);
			}
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				// TODO Auto-generated method stub
				super.onReceivedError(view, errorCode, description, failingUrl);
				webView.setVisibility(View.INVISIBLE);
				Log.e(this.getClass().getSimpleName(),description);
			}
			
		});
		// webView.loadUrl("http://www.baidu.com");
		webView.loadUrl("http://3g.163.com/ntes/special/0034073A/wechat_article.html?docid=APK09NSS00963VRO&spst=0&spss=newsapp&spsf=qq&spsw=1");

		listView = (ListView) findViewById(R.id.listView1);
		listView.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, new String[] { "aaaa",
						"bbb", "ccc", "ddd" }));
		
		UIHelper.setListViewHeightBasedOnChildren(listView);
	}
	
	
}
